package main

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
)

type Monster struct {
    Name  string
    Age   int
    Skill string
}

func main() {
    // 连接redis
    client := redis.NewClient(&redis.Options{
        Addr:     "192.168.57.133:6379",
        Password: "", 
    })

    // 输入3个怪物信息
    var monsters [3]Monster
    for i := 0; i < 3; i++ {
        fmt.Printf("请输入怪物%d信息: \n", i+1)
        fmt.Print("Name: ")
        fmt.Scanln(&monsters[i].Name)
        fmt.Print("Age: ")
        fmt.Scanln(&monsters[i].Age)
        fmt.Print("Skill: ")
        fmt.Scanln(&monsters[i].Skill)
    }

    // 存入redis hash
    for i := 0; i < 3; i++ {
        key := monsters[i].Name
        client.HSet(key, "Name", monsters[i].Name, "Age", monsters[i].Age, "Skill", monsters[i].Skill)
    }

    // 遍历所有key
    keys := client.Keys("*").Val()
    for _, key := range keys {
        // 根据key取出hash的值
        name, _ := client.HGet(key, "Name").Result()
        age, _  := client.HGet(key, "Age").Result()
        skill, _ := client.HGet(key, "Skill").Result()

        // 打印信息
        fmt.Printf("Name: %s, Age: %s, Skill: %s\n", name, age, skill) 
    }
}
